package com.sarc.games;

import java.util.TreeMap;

public class Timeline extends GameAction
{
	TreeMap<Long, TimelineAction> actions;
	long startFrameTime = 0;
	public Timeline(long timeAllowed)
	{
		super(timeAllowed);
		actions = new TreeMap<Long, TimelineAction>();
	}
	
	public void addAction(long time, TimelineAction action)
	{
		actions.put(time, action);
	}
	
	public void update(long time)
	{
		if (resolve(time))
		{
			long currentFrameTime = time - startFrameTime;
			for (boolean shouldEnd = false; !shouldEnd && actions.size() > 0; )
			{
				long actionTime = (Long) actions.keySet().toArray()[0];
				if (actionTime < currentFrameTime)
				{
					actions.get(actionTime).doAction();
					actions.remove(actionTime);
				}
				else
				{
					shouldEnd = true;
				}
			}
		}
		else
		{
			startFrameTime = time;
		}
	}
	
	public double getCurrentProgress()
	{
		return currentProgress();
	}
	
	public long timeSinceLastFrame()
	{
		long timePassed = (long)(deltaProgress() * (double)duration());
		return timePassed;
	}
	
	public interface TimelineAction
	{
		public void doAction();
	}
}

